//
//  BanJiKeShiView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/15.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BanJiKeShiView.h"
#import "SYRequestAPI+Teach.h"
#define CellSP 20*fw
#define CellW 50*fw
@implementation BanJiKeShiView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code 
}
*/
- (UICollectionView *)mainCollectionView {
    if (!_mainCollectionView) {
        //自动网格布局
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, _mainw, APP_HEIGHT-84) collectionViewLayout:flowLayout];
        _mainCollectionView.delegate = self;
        _mainCollectionView.dataSource = self;
        _mainCollectionView.backgroundColor = [UIColor whiteColor];
        [_mainCollectionView registerClass:[BanJiKeShiCell class] forCellWithReuseIdentifier:@"BanJiKeShiCell"];
        [_mainCollectionView registerClass:[BanJiKeShiHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
        [_mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"fooderView"];
    }
    return _mainCollectionView;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self initSubView];
    }
    return self;
}
- (void)initSubView{
    Type = YES;
    pageSize = 30;
    pageIndex = 1;
    [self addSubview:self.mainCollectionView];
    [_mainCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeView:) name:@"ChangeCellSpaNotice" object:nil];
    _dataArray = [NSMutableArray new];
    [_mainCollectionView headerRefreshBlock:^{
        self->pageIndex = 1;
        [self getBanJiKeShiRequest];
    }];
    [_mainCollectionView fooderRefreshBlock:^{
        self->pageIndex = ++self->pageIndex;
        [self getBanJiKeShiRequest];
    }];
}
- (void)changeView:(NSNotification *)info{
    BOOL type = [[info.userInfo objectForKey:@"Type"] intValue];
    if (type == YES) {
        Type = NO;
    }else{
        Type = YES;
    }
    [self.mainCollectionView reloadData];
}
#pragma mark == collectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(200*fw, 105*fh);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (Type == YES) {
        return UIEdgeInsetsMake(CellSP, CellSP, CellSP,CellSP);
    }else{
        return UIEdgeInsetsMake(CellSP, CellW, CellSP,CellW);
    }
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(_mainw, 131);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(_mainw, 0);
}
//设置分区数（必须实现）
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//设置每个分区的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}
//设置返回每个item的属性必须实现）
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BanJiKeShiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BanJiKeShiCell" forIndexPath:indexPath];
    BanJiKeShiModel *model = _dataArray[indexPath.row];
    [cell setValueForTitleLabel:[NSString stringWithFormat:@"%@ %@",model.courseName,model.lessonName]];
    [cell setValueForMentorTeacherLabel:model.teacherName];
    [cell setValueForChuQingLabel:[NSString stringWithFormat:@"%@",model.studentNums]];
    [cell setValueForAddressLabel:[NSString stringWithFormat:@"%@ %@",model.campusName,model.classroom]];
    [cell setValueForTitleLabel:model.attendDate];
    return cell;
}
//对头视图或者尾视图进行设置
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        headerView.frame = CGRectMake(0, 0,self.frame.size.width, 131);
        return headerView;
    } else {
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"fooderView" forIndexPath:indexPath];
        return view;
    }
}
//是否允许移动Item
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0)
{
    return YES;
}
//移动Item时触发的方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0);
{
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BanJiKeShiModel *model = _dataArray[indexPath.row];
//    BanJiKeShiModel
    if ([model.isAttend isEqualToString:@"1"]) {
        //前往课时考勤详情
        [self.mainDelegate addKeShiKaoQing:model];
    }else{
       //前往课时详情
        [self.mainDelegate addKeShiXiangQing:model];
    }
    
}

- (void)updateModel{
    [headerView.banJiKeShiView setTtitleLabelText:_mainModel.className];
    [headerView.banJiKeShiView setDangQianJinDuLabelText:_mainModel.schedule];
    [headerView.banJiKeShiView setKeChengLabelText:self.courseName];
    [headerView.banJiKeShiView setRenKeLaoShiLabelText:self.renKeLaoShiName];
    [headerView.banJiKeShiView setNianJiLabelText:_mainModel.typeName];
    [headerView.banJiKeShiView setShangKeShiJianLabelText:_mainModel.openDate];
    [headerView.banJiKeShiView setKeMuLabelText:_mainModel.statusName];
    [headerView.banJiKeShiView setZhaoShengRenShuLabelext:[NSString stringWithFormat:@"%@/%@",self.students,_mainModel.maxStudents]];
    [headerView.banJiKeShiView setJiaoShiLabelText:[NSString stringWithFormat:@"%@-%@",_mainModel.campusName,_mainModel.classroom]];
    headerView.banJiKeShiView.jieYeShiJianLabel.hidden = YES;
//    [headerView.banJiKeShiView setJieYeShiJianLabelText:_mainModel.finishedDate];
}
- (void)reloadIndex{
    pageIndex = 1;
    [_dataArray removeAllObjects];
    [self getBanJiKeShiRequest];
}
#pragma mark - 教学-班级-班级课时
- (void)getBanJiKeShiRequest{
    if (_classID.length == 0) {
        _classID = @"0";
    }
    NSDictionary *parama = @{@"classId":self.classID,
                             @"pageIndex":[NSString stringWithFormat:@"%d",pageIndex],
                             @"pageSize":[NSString stringWithFormat:@"%d",pageSize]};
    [MBProgressHUD mj_hideHUDForView:self.mainCollectionView];
    if (_dataArray.count==0) {
        [MBProgressHUD mj_showMessage:nil toView:self.mainCollectionView];
    }
    
    [SYRequestAPI sy_getClassServiceClassLessonList:parama success:^(id  _Nonnull responseObject) {
      //  NSLog(@"教学-班级-班级课时-数据 %@",responseObject);
        if (self->pageIndex == 1) {
            [self->_dataArray removeAllObjects];
        }
        NSArray *array = [responseObject objectForKey:@"data"];
//        if (array.count>0) {
//            self->pageIndex ++;
//        }
        for (NSDictionary *dic in array) {
            BanJiKeShiModel *model = [BanJiKeShiModel yy_modelWithJSON:dic];
            [self->_dataArray addObject:model];
        }
        [self.mainCollectionView reloadData];
        [self->_mainCollectionView.mj_header endRefreshing];
        [self->_mainCollectionView.mj_footer endRefreshing];
        [MBProgressHUD mj_hideHUDForView:self.mainCollectionView];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.mainCollectionView];
        [self->_mainCollectionView.mj_header endRefreshing];
        [self->_mainCollectionView.mj_footer endRefreshing];
    }];
}
@end
